RAIS  3.2
C:/Projekte/RAIS/dataaccesslayer/TableManagement.cs
Go to the documentation of this file.
00001 
00028 using System;
00029 using System.Collections.Generic;
00030 using System.Data;
00031 using System.Data.SqlClient;
00032 using System.Text;
00033 using System.Xml;
00034 
00035 using RAIS.Common.DynamicMaskManagement;
00036 using RAIS.Common.TableManagement;
00037 
00038 using TableTypes = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Table.TableTypeData>;
00039 using FieldTypes = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Field.FieldTypeData>;
00040 using FieldSizes = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Field.FieldSizeData>;
00041 using NecessityTypes = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Field.NecessityTypeData>;
00042 using TableGroups = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.TableGroup>;
00043 using Tables = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Table>;
00044 using Fields = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Field>;
00045 using QueryTypes = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Query.QueryTypeData>;
00046 using Queries = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.Query>;
00047 using QueryParameterTypes = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.QueryParameter.QueryParameterTypeData>;
00048 //using QueryParameterTypes = System.Collections.Generic.Dictionary<RAIS.Common.TableManagement.QueryParameter.QueryParameterType, RAIS.Common.TableManagement.QueryParameter.QueryParameterTypeData>;
00049 using QueryParameters = System.Collections.Generic.Dictionary<string, RAIS.Common.TableManagement.QueryParameter>;
00050 
00051 namespace RAIS.DataAccessLayer
00052 {
00056     public class TableManagement
00057         {
00058                 //*********************************************************************
00059                 #region Properties
00060 
00067         public static FieldTypes FieldTypes
00068         {
00069             get { return GetFieldTypes(); }
00070         }
00078         public static FieldSizes FieldSizes
00079         {
00080             get { return GetFieldSizes(); }
00081         }
00089                 public static NecessityTypes NecessityTypes
00090                 {
00091                         get { return GetNecessityTypes(); }
00092                 }
00100         public static TableTypes TableTypes
00101         {
00102             get { return GetTableTypes(); }
00103         }
00111         public static TableGroups TableGroups
00112         {
00113             get { return GetTableGroups(); }
00114         }
00122                 public static Tables AllTables
00123                 {
00124                         get { return GetAllTables(); }
00125                 }
00133                 public static QueryTypes QueryTypes
00134                 {
00135                         get { return GetQueryTypes(); }
00136                 }
00144                 public static QueryParameterTypes QueryParameterTypes
00145                 {
00146                         get { return GetQueryParameterTypes(); }
00147                 }
00148                 #endregion
00149                 //*********************************************************************
00150                 #region Constructor
00151                 static TableManagement()
00152         {
00153                         Table.TableTypesDictionary = GetTableTypes();
00154                         Field.FieldTypesDictionary = GetFieldTypes();
00155                         Field.FieldSizesDictionary = GetFieldSizes();
00156                         Field.NecessityTypesDictionary = GetNecessityTypes();
00157                         Query.QueryTypesDictionary = GetQueryTypes();
00158                         QueryParameter.QueryParameterTypesDictionary = GetQueryParameterTypes();
00159 
00160                         if (DynamicMask.DynamicMaskTypesDictionary == null)
00161                                 new DynamicMaskManagement();
00162                 }
00163         #endregion
00164                 //*********************************************************************
00165                 #region Public static methods
00166         public static bool IsEmptyFelds(string tableName, string fieldName)
00167         {
00168             try
00169             {
00170                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00171                 {
00172                     sqlConnection.Open();
00173 
00174                     using (SqlCommand sqlCommand = new SqlCommand(string.Format(Constants.QUERY_TEXT_HAS_ROWS, tableName), sqlConnection))
00175                     {
00176                         object result = sqlCommand.ExecuteScalar();
00177                         return !(result is DBNull) && ((int)result) == 0;
00178                     }
00179                 }
00180             }
00181             catch
00182             {
00183                 return false;
00184             }
00185         }
00193                 public static bool SaveTableGroup(TableGroup tableGroup, ref string returnMessage)
00194                 {
00195                         try
00196                         {
00197                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00198                                 {
00199                                         sqlConnection.Open();
00200 
00201                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateTableGroup, sqlConnection))
00202                                         {
00203                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00204                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableGroupID, tableGroup.ID);
00205                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Accepted, tableGroup.Accepted);
00206                                                 sqlCommand.ExecuteNonQuery();
00207                                         }
00208                                 }
00209                 returnMessage = RAIS.Common.Messages.TABLE_GROUP_ADDED;
00210                 return true;
00211                         }
00212                         catch (Exception excep)
00213                         {
00214                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
00215                                 return false;
00216                         }
00217                 }
00223                 public static Tables GetCustomTablesByTableGroup(TableGroup tableGroup)
00224                 {
00225                         Tables tables = new Tables();
00226 
00227                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00228                         {
00229                                 sqlConnection.Open();
00230 
00231                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTablesByTableGroupID, sqlConnection))
00232                                 {
00233                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00234                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableGroupID, tableGroup.ID);
00235                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_CustomTable, 1);
00236 
00237                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00238                                         {
00239                                                 while (sqlReader.Read())
00240                                                 {
00241                                                         Table table = new Table(sqlReader);
00242                                                         table.Fields = GetFieldsByTable(table);
00243                                                         tables.Add(table.Name, table);
00244                                                 }
00245                                         }
00246                                 }
00247                         }
00248                         return tables;
00249                 }
00255                 public static Tables GetTablesByTableGroup(TableGroup tableGroup, bool showProtectors, bool showEvaluators)
00256                 {
00257                         Tables tables = new Tables();
00258 
00259                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00260                         {
00261                                 sqlConnection.Open();
00262 
00263                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTablesByTableGroupID, sqlConnection))
00264                                 {
00265                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00266                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableGroupID, tableGroup.ID);
00267                                         //if (designMode)
00268                                         //    DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Hidden, null);
00269                                         if (showProtectors)
00270                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_ShowProtectors, 1);
00271                                         if (showEvaluators)
00272                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_ShowEvaluators, 1);
00273 
00274                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00275                                         {
00276                                                 while (sqlReader.Read())
00277                                                 {
00278                                                         Table table = new Table(sqlReader);
00279                                                         table.Fields = GetFieldsByTable(table);
00280                                                         tables.Add(table.Name, table);
00281                                                 }
00282                                         }
00283                                 }
00284                         }
00285                         return tables;
00286                 }
00292                 public static Tables GetAllTables()
00293                 {
00294                         Tables tables = new Tables();
00295 
00296                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00297                         {
00298                                 sqlConnection.Open();
00299 
00300                                 // Call usp_GetTablesByTableGroupID without parameters
00301                                 // stored procedure returns all tables for all groups in this case
00302                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTablesByTableGroupID, sqlConnection))
00303                                 {
00304                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00305 
00306                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00307                                         {
00308                                                 while (sqlReader.Read())
00309                                                 {
00310                                                         Table table = new Table(sqlReader);
00311                                                         table.Fields = GetFieldsByTable(table);
00312                                                         tables.Add(table.Name, table);
00313                                                 }
00314                                         }
00315                                 }
00316                         }
00317                         return tables;
00318                 }
00324         public static Table GetTableByTableID(int id, SqlConnection sqlConnection)
00325         {
00326             Table table = new Table();
00327 
00328             using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTableByTableID, sqlConnection))
00329             {
00330                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00331                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, id);
00332 
00333                 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00334                 {
00335                     sqlReader.Read();
00336 
00337                     if (sqlReader.HasRows)
00338                     {
00339                         table = new Table(sqlReader);
00340                     }
00341                 }
00342             }
00343             table.Fields = GetFieldsByTable(table, sqlConnection);
00344 
00345             return table;
00346         }
00352         public static Table GetTableByTableID(int id)
00353         {
00354             Table table = new Table();
00355 
00356             using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00357             {
00358                 sqlConnection.Open();
00359                 return GetTableByTableID(id, sqlConnection);
00360             }
00361         }
00367                 public static Table GetTableWithRelatedFieldsByTableID(int id)
00368                 {
00369                         Table table = new Table();
00370 
00371                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00372                         {
00373                                 sqlConnection.Open();
00374 
00375                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTableByTableID, sqlConnection))
00376                                 {
00377                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00378                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, id);
00379 
00380                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00381                                         {
00382                                                 sqlReader.Read();
00383 
00384                                                 if (sqlReader.HasRows)
00385                                                 {
00386                                                         table = new Table(sqlReader);
00387                                                         table.Fields = GetFieldsWithRelatedByTable(table);
00388                                                 }
00389                                         }
00390                                 }
00391                         }
00392                         return table;
00393                 }
00399                 public static Table GetTableWithRelatedFieldsByTableName(string tableName)
00400                 {
00401                         Table table = new Table();
00402 
00403                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00404                         {
00405                                 sqlConnection.Open();
00406 
00407                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTableByTableName, sqlConnection))
00408                                 {
00409                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00410                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableName, tableName);
00411 
00412                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00413                                         {
00414                                                 sqlReader.Read();
00415 
00416                                                 if (sqlReader.HasRows)
00417                                                 {
00418                                                         table = new Table(sqlReader);
00419                                                         table.Fields = GetFieldsWithRelatedByTable(table);
00420                                                 }
00421                                         }
00422                                 }
00423                         }
00424                         return table;
00425                 }
00433                 public static bool AddTable(Table table, TableGroup tableGroup, string maskName, int subcategoryID, DynamicMask.DynamicMaskType? dynamicMaskType, string dynamicMaskName, bool addForeignKeyField, bool createDynemicMasks, ref string returnMessage)
00434                 {
00435                         try
00436                         {
00437                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00438                                 {
00439                                         sqlConnection.Open();
00440 
00441                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_AddTable, sqlConnection))
00442                                         {
00443                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00444                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, table.ID, System.Data.ParameterDirection.Output);
00445                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableName, table.Name);
00446                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableGroupID, tableGroup.ID);
00447                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableTypeID, table.TypeData.ID);
00448                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_CustomTable, table.CustomTable);
00449                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_NeedsValidation, table.NeedsValidation);
00450                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_MaskName, maskName);
00451                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SubcategoryID, subcategoryID);
00452                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_DynamicMaskName, dynamicMaskName);
00453                                                 if (dynamicMaskType == null)
00454                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_DynamicMaskTypeID, null);
00455                                                 else
00456                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_DynamicMaskTypeID, DynamicMask.DynamicMaskTypesDictionary[(DynamicMask.DynamicMaskType)dynamicMaskType].ID);
00457                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_AddForeignKeyField, (addForeignKeyField) ? 1 : 0);
00458                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_CreateDynamicMask, createDynemicMasks);
00459                                                 
00460                                                 sqlCommand.ExecuteNonQuery();
00461 
00462                                                 table.ID = int.Parse(sqlCommand.Parameters[Constants.SP_PARAMETER_TableID].Value.ToString());
00463                                         }
00464                                 }
00465                 returnMessage = RAIS.Common.Messages.TABLE_ADDED;
00466                 return true;
00467                         }
00468                         catch (Exception excep)
00469                         {
00470                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
00471                                 return false;
00472                         }
00473                 }
00480                 public static bool RemoveTable(Table table, ref string returnMessage)
00481                 {
00482                         try
00483                         {
00484                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00485                                 {
00486                                         sqlConnection.Open();
00487 
00488                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_DeleteTable, sqlConnection))
00489                                         {
00490                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00491                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, table.ID);
00492 
00493                                                 sqlCommand.ExecuteNonQuery();
00494                                         }
00495                                 }
00496                 returnMessage = RAIS.Common.Messages.TABLE_REMOVED;
00497                 return true;
00498                         }
00499                         catch (Exception excep)
00500                         {
00501                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
00502                                 return false;
00503                         }
00504                 }
00510         public static Fields GetFieldsByTable(Table table)
00511                 {
00512                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00513                         {
00514                                 sqlConnection.Open();
00515                 return GetFieldsByTable(table, sqlConnection);
00516                         }
00517                 }
00523         public static Fields GetFieldsByTable(Table table, SqlConnection sqlConnection)
00524         {
00525             Fields fields = new Fields();
00526             using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetFieldsByTableID, sqlConnection))
00527             {
00528                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00529                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, table.ID);
00530 
00531                 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00532                 {
00533                     while (sqlReader.Read())
00534                     {
00535                         Field field = new Field(sqlReader);
00536                         field.Table = table;
00537                         fields.Add(field.Name, field);
00538                     }
00539                 }
00540             }
00541             return fields;
00542         }
00548                 public static Fields GetFieldsWithRelatedByTable(Table table)
00549                 {
00550                         Fields fields = new Fields();
00551 
00552                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00553                         {
00554                                 sqlConnection.Open();
00555 
00556                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetFieldsByTableID, sqlConnection))
00557                                 {
00558                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00559                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, table.ID);
00560                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_WithRelatedFields, 1);
00561 
00562                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00563                                         {
00564                                                 while (sqlReader.Read())
00565                                                 {
00566                                                         Field field = new Field(sqlReader);
00567                             field.Table=table;
00568                                                         fields.Add(field.Name, field);
00569                                                 }
00570                                         }
00571                                 }
00572                         }
00573 
00574                         return fields;
00575                 }
00581                 public static Field GetFieldByID(int fieldID)
00582                 {
00583                         Field field = null;
00584 
00585                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00586                         {
00587                                 sqlConnection.Open();
00588 
00589                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetFieldByID, sqlConnection))
00590                                 {
00591                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00592                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldID, fieldID);
00593 
00594                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00595                                         {
00596                                                 sqlReader.Read();
00597 
00598                                                 if (sqlReader.HasRows)
00599                                                 {
00600                                                         field = new Field(sqlReader);
00601                                                         if (field.RelatedTable != null)
00602                                                                 field.RelatedTable.Fields = GetFieldsByTable(field.RelatedTable);
00603                                                 }
00604                                         }
00605                                 }
00606                         }
00607 
00608                         return field;
00609                 }
00617         public static bool AddField(Table table, Field field, ref string returnMessage)
00618                 {
00619                         try
00620                         {
00621                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00622                                 {
00623                                         sqlConnection.Open();
00624 
00625                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_AddField, sqlConnection))
00626                                         {
00627                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00628                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldID, field.ID, System.Data.ParameterDirection.Output);
00629                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldName, field.Name);
00630                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_LocalLanguageFieldName, field.LocalLanguageFieldName);
00631                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldPurpose, field.FieldPurpose);
00632                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_LocalLanguageFieldPurpose, field.LocalLanguageFieldPurpose);
00633                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, table.ID);
00634                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldTypeID, field.TypeData.ID);
00635                         if (field.SizeData == null)
00636                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldSizeID, null);
00637                         else
00638                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldSizeID, field.SizeData.ID);
00639                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_NecessityTypeID, field.NecessityData.ID);
00640                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Order, field.Order);
00641                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_IsNameField, field.IsNameField);
00642                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SystemField, field.SystemField);
00643                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_VisibleOnForms, field.VisibleOnForms);
00644                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_UniqueValue, field.UniqueValue);
00645                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_NeedsTranslation, field.NeedsTranslation);
00646                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Multiline, field.Multiline);
00647                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_HighField, field.HighField);
00648                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_ReadOnly, field.ReadOnly);
00649                                                 if (field.RelatedTable == null)
00650                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RelatedTableID, null);
00651                                                 else
00652                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RelatedTableID, field.RelatedTable.ID);
00653 
00654                                                 sqlCommand.ExecuteNonQuery();
00655 
00656                                                 field.ID = int.Parse(sqlCommand.Parameters[Constants.SP_PARAMETER_FieldID].Value.ToString());
00657                                         }
00658                                 }
00659                 returnMessage = RAIS.Common.Messages.TABLE_FIELD_ADDED;
00660                 return true;
00661                         }
00662                         catch (Exception excep)
00663                         {
00664                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
00665                                 return false;
00666                         }
00667                 }
00674                 public static bool SaveField(Field field, ref string returnMessage)
00675                 {
00676                         try
00677                         {
00678                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00679                                 {
00680                                         sqlConnection.Open();
00681 
00682                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateField, sqlConnection))
00683                                         {
00684                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00685                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldID, field.ID);
00686                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldName, field.Name);
00687                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_LocalLanguageFieldName, field.LocalLanguageFieldName);
00688                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldPurpose, field.FieldPurpose);
00689                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_LocalLanguageFieldPurpose, field.LocalLanguageFieldPurpose);
00690                                                 if (field.SizeData == null)
00691                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldSizeID, null);
00692                                                 else
00693                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldSizeID, field.SizeData.ID);
00694                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_NecessityTypeID, field.NecessityData.ID);
00695                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_IsNameField, field.IsNameField);
00696                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_SystemField, field.SystemField);
00697                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_VisibleOnForms, field.VisibleOnForms);
00698                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_UniqueValue, field.UniqueValue);
00699                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_NeedsTranslation, field.NeedsTranslation);
00700                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Multiline, field.Multiline);
00701                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_HighField, field.HighField);
00702                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_ReadOnly, field.ReadOnly);
00703                                                 if (field.RelatedTable == null)
00704                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RelatedTableID, null);
00705                                                 else
00706                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RelatedTableID, field.RelatedTable.ID);
00707 
00708                                                 sqlCommand.ExecuteNonQuery();
00709                                         }
00710                                 }
00711                 returnMessage = RAIS.Common.Messages.TABLE_FIELD_SAVED;
00712                 return true;
00713                         }
00714                         catch (Exception excep)
00715                         {
00716                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
00717                                 if (excep is System.Data.SqlClient.SqlException)
00718                                 {
00719                                         if (((SqlException)excep).Number == 515)
00720                                                 returnMessage = RAIS.Common.Messages.WRONG_NECESSITY_TYPE;
00721                                 }
00722                                 return false;
00723                         }
00724                 }
00731                 public static bool SaveFieldOrder(Field field, ref string returnMessage)
00732                 {
00733                         try
00734                         {
00735                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00736                                 {
00737                                         sqlConnection.Open();
00738 
00739                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateFieldOrder, sqlConnection))
00740                                         {
00741                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00742                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldID, field.ID);
00743                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Order, field.Order);
00744 
00745                                                 sqlCommand.ExecuteNonQuery();
00746                                         }
00747                                 }
00748                                 returnMessage = RAIS.Common.Messages.TABLE_FIELD_SAVED;
00749                                 return true;
00750                         }
00751                         catch (Exception excep)
00752                         {
00753                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
00754                                 return false;
00755                         }
00756                 }
00763                 public static bool SaveFieldQueryName(Field field, ref string returnMessage)
00764                 {
00765                         try
00766                         {
00767                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00768                                 {
00769                                         sqlConnection.Open();
00770 
00771                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateFieldQueryName, sqlConnection))
00772                                         {
00773                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00774                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldID, field.ID);
00775                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryName, field.QueryName);
00776 
00777                                                 sqlCommand.ExecuteNonQuery();
00778                                         }
00779                                 }
00780                                 returnMessage = RAIS.Common.Messages.TABLE_FIELD_SAVED;
00781                                 return true;
00782                         }
00783                         catch (Exception excep)
00784                         {
00785                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
00786                                 return false;
00787                         }
00788                 }
00795                 public static bool RemoveField(Field field, ref string returnMessage)
00796                 {
00797                         try
00798                         {
00799                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00800                                 {
00801                                         sqlConnection.Open();
00802 
00803                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_DeleteField, sqlConnection))
00804                                         {
00805                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00806                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_FieldID, field.ID);
00807 
00808                                                 sqlCommand.ExecuteNonQuery();
00809                                         }
00810                                 }
00811                 returnMessage = RAIS.Common.Messages.TABLE_FIELD_REMOVED;
00812                 return true;
00813                         }
00814                         catch (Exception excep)
00815                         {
00816                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
00817                                 return false;
00818                         }
00819                 }
00825                 public static FieldSizes GetFieldSizesByFieldType(Field.FieldType fieldType)
00826                 {
00827                         FieldSizes fieldSizes = new FieldSizes();
00828 
00829                         foreach (KeyValuePair<string, Field.FieldSizeData> fieldSizePair in Field.FieldSizesDictionary)
00830                                 if (fieldSizePair.Value.FieldType == fieldType)
00831                                         fieldSizes.Add(fieldSizePair.Key, fieldSizePair.Value);
00832 
00833                         return fieldSizes;
00834                 }
00840                 public static XmlDocument GetTableAndFieldXml()
00841                 {
00842                         XmlDocument xmlDoc = null;
00843 
00844                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00845                         {
00846                                 sqlConnection.Open();
00847 
00848                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GenerateTablesScript, sqlConnection))
00849                                 {
00850                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00851                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00852                                         {
00853                                                 sqlReader.Read();
00854 
00855                                                 if (sqlReader.HasRows)
00856                                                 {
00857                                                         xmlDoc = new XmlDocument();
00858                                                         xmlDoc.LoadXml("<Tables>" + sqlReader[0].ToString() + "</Tables>");
00859 
00860                                                 }
00861                                         }
00862                                 }
00863                         }
00864                         return xmlDoc;
00865                 }
00871         public static Queries GetQueries()
00872                 {
00873             return GetQueries(null);
00874                 }
00880                 public static Query GetQueryByQueryID(int id)
00881                 {
00882                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00883                         {
00884                                 sqlConnection.Open();
00885                 return GetQueryByQueryID(id, sqlConnection);
00886                         }
00887                 }
00888 
00894         public static Query GetQueryByQueryID(int id, SqlConnection sqlConnection)
00895         {
00896             Query query = new Query();
00897 
00898             using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetQueryByQueryID, sqlConnection))
00899             {
00900                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00901                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryID, id);
00902 
00903                 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00904                 {
00905                     sqlReader.Read();
00906 
00907                     if (sqlReader.HasRows)
00908                     {
00909                         query = new Query(sqlReader);
00910                     }
00911                 }
00912             }
00913 
00914             query.QueryParameters = GetQueryParametersByQuery(query);
00915             return query;
00916         }
00917 
00923                 public static Query GetQueryByQueryName(string queryName)
00924                 {
00925                         Query query = new Query();
00926 
00927                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00928                         {
00929                                 sqlConnection.Open();
00930 
00931                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetQueryByQueryName, sqlConnection))
00932                                 {
00933                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00934                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryName, queryName);
00935 
00936                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00937                                         {
00938                                                 sqlReader.Read();
00939 
00940                                                 if (sqlReader.HasRows)
00941                                                 {
00942                                                         query = new Query(sqlReader);
00943                                                         query.QueryParameters = GetQueryParametersByQuery(query);
00944                                                 }
00945                                         }
00946                                 }
00947                         }
00948                         return query;
00949                 }
00955                 public static Queries GetQueriesByQueryType(Query.QueryType queryType)
00956                 {
00957                         Query.QueryTypeData queryTypeData = Query.GetQueryTypeDataByQueryType(queryType);
00958             return GetQueries(queryTypeData);
00959                 }
00965                 public static List<string> GetQueryAssignedObjectsNames(Query query)
00966                 {
00967                         List<string> assignedObjectsNames = new List<string>();
00968 
00969             if (query.Type == Query.QueryType.DataRoleRestriction)
00970             {
00971                 assignedObjectsNames = DataAccessLayer.DataRoleRestrictionManagement.GetQueryAssignedRestrictions(query.Name);
00972             }
00973             else
00974             {
00975                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
00976                 {
00977                     sqlConnection.Open();
00978 
00979                     using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetQueryAssignedObjects, sqlConnection))
00980                     {
00981                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
00982                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryID, query.ID);
00983 
00984                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
00985                         {
00986                             while (sqlReader.Read())
00987                             {
00988                                 string objectName = sqlReader["Assigned Object Name"].ToString();
00989                                 if (!assignedObjectsNames.Contains(objectName))
00990                                     assignedObjectsNames.Add(objectName);
00991                             }
00992                         }
00993                     }
00994                 }
00995             }
00996 
00997                         return assignedObjectsNames;
00998                 }
01005                 public static bool AddQuery(Query query, ref string returnMessage)
01006                 {
01007                         try
01008                         {
01009                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01010                                 {
01011                                         sqlConnection.Open();
01012 
01013                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_AddQuery, sqlConnection))
01014                                         {
01015                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01016                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryID, query.ID, System.Data.ParameterDirection.Output);
01017                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryName, query.Name);
01018                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryText, query.QueryText);
01019                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryTypeID, query.TypeData.ID);
01020                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParametersXML, query.NewParameterNamesInXml);
01021 
01022                                                 sqlCommand.ExecuteNonQuery();
01023 
01024                                                 query.ID = int.Parse(sqlCommand.Parameters[Constants.SP_PARAMETER_QueryID].Value.ToString());
01025                                         }
01026                                 }
01027                                 returnMessage = RAIS.Common.Messages.QUERY_SAVED;
01028                                 return true;
01029                         }
01030                         catch (Exception excep)
01031                         {
01032                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01033                                 return false;
01034                         }
01035                 }
01042                 public static bool SaveQuery(Query query, ref string returnMessage)
01043                 {
01044                         try
01045                         {
01046                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01047                                 {
01048                                         sqlConnection.Open();
01049 
01050                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateQuery, sqlConnection))
01051                                         {
01052                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01053                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryID, query.ID);
01054                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryName, query.Name);
01055                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryText, query.QueryText);
01056                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryTypeID, query.TypeData.ID);
01057                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParametersXML, query.NewParameterNamesInXml);
01058 
01059                                                 sqlCommand.ExecuteNonQuery();
01060                                         }
01061                                 }
01062                                 returnMessage = RAIS.Common.Messages.QUERY_SAVED;
01063                                 return true;
01064                         }
01065                         catch (Exception excep)
01066                         {
01067                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01068                                 return false;
01069                         }
01070                 }
01077                 public static bool RemoveQuery(Query query, ref string returnMessage)
01078                 {
01079                         try
01080                         {
01081                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01082                                 {
01083                                         sqlConnection.Open();
01084 
01085                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_DeleteQuery, sqlConnection))
01086                                         {
01087                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01088                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryID, query.ID);
01089 
01090                                                 sqlCommand.ExecuteNonQuery();
01091                                         }
01092                                 }
01093                                 returnMessage = RAIS.Common.Messages.QUERY_REMOVED;
01094                                 return true;
01095                         }
01096                         catch (Exception excep)
01097                         {
01098                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01099                                 return false;
01100                         }
01101                 }
01108                 public static bool CreateQuery(Query query, ref string returnMessage)
01109                 {
01110                         bool success = true;
01111                         DataSet dataSet = new DataSet();
01112                         SqlDataAdapter dataAdapter = new SqlDataAdapter(string.Empty, DataAccessUtilities.ConnectionString);
01113                         StringBuilder sqlStr = new StringBuilder();
01114 
01115                         if (query != null)
01116                         {
01117                                 if (query.Type == Query.QueryType.RanAutogeneration)
01118                                 {
01119                                         success = CreateStoredProcedure(query, dataSet, dataAdapter, ref returnMessage);
01120                                 }
01121                                 else
01122                                 {
01123                                         success = ValidateForbiddenCommandsInQuery(query, dataSet, dataAdapter, ref returnMessage);
01124 
01125                                         if (success)
01126                                         {
01127                                                 // Use test query execution to get the result record set
01128                                                 // Use this record set for columns definition during function creation
01129                                                 // Add DECLARE for each parameter
01130                                                 bool firstParameter = true;
01131                                                 foreach (KeyValuePair<string, QueryParameter> kvp in query.QueryParameters)
01132                                                 {
01133                                                         QueryParameter parameter = kvp.Value;
01134                                                         sqlStr.AppendLine(string.Format("{0} @{1} {2}"
01135                                                                 , (firstParameter) ? "DECLARE" : "      ,"
01136                                                                 , parameter.Name
01137                                                                 , GetSqlTypeByParameterType(parameter.Type)));
01138                                                         firstParameter = false;
01139                                                 }
01140 
01141                                                 // Add SET for each parameter
01142                                                 firstParameter = true;
01143                                                 foreach (KeyValuePair<string, QueryParameter> kvp in query.QueryParameters)
01144                                                 {
01145                                                         QueryParameter parameter = kvp.Value;
01146                                                         sqlStr.AppendLine(string.Format("SET @{0} = {1}"
01147                                                                 , parameter.Name
01148                                                                 , GetParameterValueByParameterType(parameter.Type)));
01149                                                         firstParameter = false;
01150                                                 }
01151 
01152                                                 // Add QueryText
01153                                                 sqlStr.AppendLine(string.Empty);
01154                                                 sqlStr.AppendLine(query.QueryText);
01155 
01156                                                 //SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStr.ToString(), DataAccessUtilities.ConnectionString);
01157                                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
01158                                                 try
01159                                                 {
01160                                                         dataAdapter.Fill(dataSet);
01161                                                 }
01162                                                 catch (Exception e)
01163                                                 {
01164                                                         success = false;
01165                             returnMessage = e.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01166                                                 }
01167 
01168                                                 if (success)
01169                                                 {
01170                                                         sqlStr.Length = 0;
01171                                                         try
01172                                                         {
01173                                                                 // Drop function if exists
01174                                                                 sqlStr.AppendLine(string.Format("IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{0}]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))", query.Name));
01175                                                                 sqlStr.AppendLine(string.Format("       DROP FUNCTION [dbo].[{0}]", query.Name));
01176                                                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
01177                                                                 dataAdapter.Fill(dataSet);
01178 
01179                                                                 // Create function
01180                                                                 sqlStr.Length = 0;
01181                                                                 sqlStr.AppendLine(string.Format("/******************************************************************************"));
01182                                                                 sqlStr.AppendLine(string.Format("**  Function Name: [{0}]", query.Name));
01183                                                                 sqlStr.AppendLine(string.Format("******************************************************************************/"));
01184                                                                 sqlStr.AppendLine(string.Format("CREATE Function [{0}](", query.Name));
01185 
01186                                                                 // Add parameters
01187 
01188                                 sqlStr.AppendLine(CreateParametersBlock(query));
01189 
01190                                                                 sqlStr.AppendLine(string.Format(")"));
01191                                                                 sqlStr.AppendLine(string.Format("RETURNS @ResultTable TABLE"));
01192                                                                 sqlStr.AppendLine(string.Format("("));
01193 
01194                                                                 // Add return fields declaration
01195                                                                 bool firstColumn = true;
01196                                                                 foreach (DataColumn column in dataSet.Tables[0].Columns)
01197                                                                 {
01198                                                                         sqlStr.AppendLine(string.Format("       {0}[{1}] {2}"
01199                                                                                 , (firstColumn) ? "  " : ", "
01200                                                                                 , column.ColumnName
01201                                                                                 , GetSqlTypeByColumnType(column.DataType.ToString())));
01202 
01203                                                                         firstColumn = false;
01204                                                                 }
01205 
01206                                                                 sqlStr.AppendLine(string.Format(")"));
01207                                                                 sqlStr.AppendLine(string.Format("AS"));
01208                                                                 sqlStr.AppendLine(string.Format("BEGIN"));
01209 
01210                                                                 sqlStr.AppendLine(string.Format("       INSERT @ResultTable"));
01211                                                                 sqlStr.AppendLine(string.Format("       {0}", query.QueryText));
01212                                                                 sqlStr.AppendLine(string.Format(""));
01213                                                                 sqlStr.AppendLine(string.Format("       RETURN"));
01214                                                                 sqlStr.AppendLine(string.Format("END"));
01215 
01216                                                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
01217                                                                 dataAdapter.Fill(dataSet);
01218 
01219                                                                 // Grant select for public
01220                                                                 sqlStr.Length = 0;
01221                                                                 sqlStr.AppendLine(string.Format("GRANT SELECT ON [{0}] TO PUBLIC", query.Name));
01222                                                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
01223                                                                 dataAdapter.Fill(dataSet);
01224 
01225                                                                 success = SaveQuery_SetCompiled(query, true, ref returnMessage);
01226 
01227                                                                 if (success)
01228                                                                         returnMessage = Common.Messages.QUERY_CREATED;
01229                                                         }
01230                                                         catch (Exception e)
01231                                                         {
01232                                                                 success = false;
01233                                 returnMessage = e.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01234                                                         }
01235                                                 }
01236                                         }
01237                                 }
01238                         }
01239                         return success;
01240                 }
01247                 public static bool CheckQuery(Query query, ref string returnMessage)
01248                 {
01249                         bool success = true;
01250                         DataSet dataSet = new DataSet();
01251                         SqlDataAdapter dataAdapter = new SqlDataAdapter(query.QueryText, DataAccessUtilities.ConnectionString);
01252                         try
01253                         {
01254                                 dataAdapter.Fill(dataSet);
01255 
01256                                 returnMessage = Common.Messages.QUERY_IS_VALID;
01257                         }
01258                         catch (Exception e)
01259                         {
01260                                 success = false;
01261                 returnMessage = e.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01262                         }
01263 
01264                         return success;
01265                 }
01271         public static QueryParameters GetQueryParametersByQuery(Query query, SqlConnection sqlConnection)
01272         {
01273             QueryParameters queryParameters = new QueryParameters();
01274             using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetQueryParametersByQueryID, sqlConnection))
01275             {
01276                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01277                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryID, query.ID);
01278 
01279                 using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01280                 {
01281                     while (sqlReader.Read())
01282                     {
01283                         QueryParameter queryParameter = new QueryParameter(sqlReader);
01284                         queryParameters.Add(queryParameter.Name, queryParameter);
01285                     }
01286                 }
01287 
01288                 foreach (var queryParameter in queryParameters.Values)
01289                 {
01290                     if (queryParameter.Table != null)
01291                         queryParameter.Table = GetTableByTableID(queryParameter.Table.ID, sqlConnection);
01292                     if (queryParameter.RelatedQuery != null)
01293                         queryParameter.RelatedQuery = GetQueryByQueryID(queryParameter.RelatedQuery.ID);
01294                 }
01295             }
01296 
01297             return queryParameters;
01298         }
01299 
01305                 public static QueryParameters GetQueryParametersByQuery(Query query)
01306                 {
01307                         QueryParameters queryParameters = new QueryParameters();
01308 
01309                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01310                         {
01311                                 sqlConnection.Open();
01312 
01313                 return GetQueryParametersByQuery(query, sqlConnection);
01314                         }
01315                 }
01323                 public static bool AddQueryParameter(Query query, QueryParameter queryParameter, ref string returnMessage)
01324                 {
01325                         try
01326                         {
01327                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01328                                 {
01329                                         sqlConnection.Open();
01330 
01331                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_AddQueryParameter, sqlConnection))
01332                                         {
01333                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01334                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParameterID, queryParameter.ID, System.Data.ParameterDirection.Output);
01335                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryID, query.ID);
01336                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParameterName, queryParameter.Name);
01337                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParameterTypeID, queryParameter.TypeData.ID);
01338                                                 if (queryParameter.Table == null)
01339                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, null);
01340                                                 else
01341                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, queryParameter.Table.ID);
01342                                                 if (queryParameter.RelatedQuery == null)
01343                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RelatedQueryID, null);
01344                                                 else
01345                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RelatedQueryID, queryParameter.RelatedQuery.ID);
01346                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Order, queryParameter.Order);
01347                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_VisibleName, queryParameter.VisibleName);
01348 
01349                                                 sqlCommand.ExecuteNonQuery();
01350 
01351                                                 queryParameter.ID = int.Parse(sqlCommand.Parameters[Constants.SP_PARAMETER_QueryParameterID].Value.ToString());
01352                                         }
01353                                 }
01354                                 returnMessage = RAIS.Common.Messages.QUERY_PARAMETER_ADDED;
01355                                 return true;
01356                         }
01357                         catch (Exception excep)
01358                         {
01359                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01360                                 return false;
01361                         }
01362                 }
01369                 public static bool SaveQueryParameter(QueryParameter queryParameter, ref string returnMessage)
01370                 {
01371                         try
01372                         {
01373                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01374                                 {
01375                                         sqlConnection.Open();
01376 
01377                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateQueryParameter, sqlConnection))
01378                                         {
01379                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01380                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParameterID, queryParameter.ID);
01381                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParameterName, queryParameter.Name);
01382                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParameterTypeID, queryParameter.TypeData.ID);
01383                                                 if (queryParameter.Table == null)
01384                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, null);
01385                                                 else
01386                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, queryParameter.Table.ID);
01387                                                 if (queryParameter.RelatedQuery == null)
01388                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RelatedQueryID, null);
01389                                                 else
01390                                                         DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_RelatedQueryID, queryParameter.RelatedQuery.ID);
01391                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Order, queryParameter.Order);
01392                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_VisibleName, queryParameter.VisibleName);
01393 
01394                                                 sqlCommand.ExecuteNonQuery();
01395                                         }
01396                                 }
01397                                 returnMessage = RAIS.Common.Messages.QUERY_PARAMETER_SAVED;
01398                                 return true;
01399                         }
01400                         catch (Exception excep)
01401                         {
01402                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01403                                 return false;
01404                         }
01405                 }
01412                 public static bool SaveQueryParameterOrder(QueryParameter queryParameter, ref string returnMessage)
01413                 {
01414                         try
01415                         {
01416                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01417                                 {
01418                                         sqlConnection.Open();
01419 
01420                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateQueryParameterOrder, sqlConnection))
01421                                         {
01422                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01423                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParameterID, queryParameter.ID);
01424                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Order, queryParameter.Order);
01425 
01426                                                 sqlCommand.ExecuteNonQuery();
01427                                         }
01428                                 }
01429                                 returnMessage = RAIS.Common.Messages.QUERY_PARAMETER_SAVED;
01430                                 return true;
01431                         }
01432                         catch (Exception excep)
01433                         {
01434                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01435                                 return false;
01436                         }
01437                 }
01444                 public static bool RemoveQueryParameter(QueryParameter queryParameter, ref string returnMessage)
01445                 {
01446                         try
01447                         {
01448                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01449                                 {
01450                                         sqlConnection.Open();
01451 
01452                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_DeleteQueryParameter, sqlConnection))
01453                                         {
01454                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01455                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryParameterID, queryParameter.ID);
01456 
01457                                                 sqlCommand.ExecuteNonQuery();
01458                                         }
01459                                 }
01460                                 returnMessage = RAIS.Common.Messages.QUERY_PARAMETER_REMOVED;
01461                                 return true;
01462                         }
01463                         catch (Exception excep)
01464                         {
01465                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01466                                 return false;
01467                         }
01468                 }
01475                 public static bool CreateHistoryTable(Table table, ref string returnMessage)
01476                 {
01477                         try
01478                         {
01479                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01480                                 {
01481                                         sqlConnection.Open();
01482 
01483                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_CreateHistoryTable, sqlConnection))
01484                                         {
01485                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01486                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_TableID, table.ID);
01487 
01488                                                 sqlCommand.ExecuteNonQuery();
01489                                         }
01490                                 }
01491                                 returnMessage = RAIS.Common.Messages.HISTORY_TABLE_ADDED;
01492                                 return true;
01493                         }
01494                         catch (Exception excep)
01495                         {
01496                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
01497                                 return false;
01498                         }
01499                 }
01500 
01501         public static bool IsTableContainData(Table table)
01502         {
01503             string query="select count(*) as [Count] from [{0}]";
01504             int count = 0;
01505             string tabName = table.Name;
01506             try
01507             {
01508                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01509                 {
01510                     sqlConnection.Open();
01511 
01512                     for (int i = 0; i < 2; i++)
01513                     {
01514                         using (SqlCommand sqlCommand = new SqlCommand(string.Format(query, table.Name), sqlConnection))
01515                         {
01516                             using (SqlDataReader reader = sqlCommand.ExecuteReader())
01517                             {
01518                                 reader.Read();
01519                                 count += (int)reader["Count"];
01520                             }
01521                         }
01522                     }
01523                     tabName = table.Name.Substring(0, table.Name.Length - 8);
01524                 }
01525             }
01526             catch (Exception excep)
01527             {
01528                 return true;
01529             }
01530             return count > 0;
01531         }
01532                 #endregion
01533                 //*********************************************************************
01534                 #region Private static helper functions
01535 
01540                 static TableTypes GetTableTypes()
01541                 {
01542                         TableTypes tableTypes = new TableTypes();
01543                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01544                         {
01545                                 sqlConnection.Open();
01546 
01547                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTableTypes, sqlConnection))
01548                                 {
01549                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01550 
01551                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01552                                         {
01553                                                 while (sqlReader.Read())
01554                                                 {
01555                                                         Table.TableTypeData tableTypeData = new Table.TableTypeData(sqlReader);
01556                                                         
01557                                                         Table.TableType tableType;
01558                                                         try
01559                                                         {
01560                                                                 // Next operator throw an exeption if we do not have 
01561                                                                 // element in enum which corresponds to table type in Table Type table
01562                                                                 // Catch it and throw exception with more clear message.
01563                                                                 tableType = tableTypeData.Type;
01564                                                         }
01565                                                         catch
01566                                                         {
01567                                                                 throw new Exception("Table type " + tableTypeData.VisibleName + " exists in DB but does not exist in Enum Table.TableType. The running ASP server is not up to date with respect to the DB.");
01568                                                         }
01569 
01570                                                         tableTypes.Add(tableTypeData.VisibleName, tableTypeData);
01571                                                 }
01572 
01573                                                 // We checked that all enum elements exist in [Table Type] table, 
01574                                                 // so throw an exeption if number of elements in enum does not 
01575                                                 // equal number of elements in [Table Type] table
01576                                                 if (Enum.GetNames(typeof(Table.TableType)).Length != tableTypes.Count)
01577                                                         throw new Exception("There are table types which exist in Enum Table.TableType but does not exist in DB. The running DB is not up to date with respect to the ASP server.");
01578                                         }
01579                                 }
01580                         }
01581 
01582                         return tableTypes;
01583                 }
01589                 static FieldTypes GetFieldTypes()
01590                 {
01591                         FieldTypes fieldTypes = new FieldTypes();
01592                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01593                         {
01594                                 sqlConnection.Open();
01595 
01596                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetFieldTypes, sqlConnection))
01597                                 {
01598                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01599 
01600                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01601                                         {
01602                                                 while (sqlReader.Read())
01603                                                 {
01604                                                         Field.FieldTypeData fieldTypeData = new Field.FieldTypeData(sqlReader);
01605 
01606                                                         Field.FieldType fieldType;
01607                                                         try
01608                                                         {
01609                                                                 // Next operator throw an exeption if we do not have 
01610                                                                 // element in enum which corresponds to field type in Field Type table
01611                                                                 // Catch it and throw exception with more clear message.
01612                                                                 fieldType = fieldTypeData.Type;
01613                                                         }
01614                                                         catch
01615                                                         {
01616                                                                 throw new Exception("Field type " + fieldTypeData.VisibleName + " exists in DB but does not exist in Enum Field.FieldType. The running ASP server is not up to date with respect to the DB.");
01617                                                         }
01618 
01619                                                         fieldTypes.Add(fieldTypeData.VisibleName, fieldTypeData);
01620                                                 }
01621 
01622                                                 // We checked that all enum elements exist in [Field Type] table, 
01623                                                 // so throw an exeption if number of elements in enum does not 
01624                                                 // equal number of elements in [Field Type] table
01625                                                 if (Enum.GetNames(typeof(Field.FieldType)).Length != fieldTypes.Count)
01626                                                         throw new Exception("There are field types which exist in Enum Field.FieldType but does not exist in DB. The running DB is not up to date with respect to the ASP server.");
01627                                         }
01628                                 }
01629                         }
01630 
01631                         return fieldTypes;
01632                 }
01638                 static FieldSizes GetFieldSizes()
01639                 {
01640                         FieldSizes fieldSizes = new FieldSizes();
01641                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01642                         {
01643                                 sqlConnection.Open();
01644 
01645                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetFieldSizes, sqlConnection))
01646                                 {
01647                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01648 
01649                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01650                                         {
01651                                                 while (sqlReader.Read())
01652                                                 {
01653                                                         Field.FieldSizeData fieldSizeData = new Field.FieldSizeData(sqlReader);
01654 
01655                                                         Field.FieldSize fieldSize;
01656                                                         try
01657                                                         {
01658                                                                 // Next operator throw an exeption if we do not have 
01659                                                                 // element in enum which corresponds to field size in Field Size table
01660                                                                 // Catch it and throw exception with more clear message.
01661                                                                 fieldSize = fieldSizeData.Type;
01662                                                         }
01663                                                         catch
01664                                                         {
01665                                                                 throw new Exception("Field size " + fieldSizeData.VisibleName + " exists in DB but does not exist in Enum Field.FieldSize. The running ASP server is not up to date with respect to the DB.");
01666                                                         }
01667 
01668                                                         fieldSizes.Add(fieldSizeData.VisibleName, fieldSizeData);
01669                                                 }
01670 
01671                                                 // We checked that all enum elements exist in [Field Size] table, 
01672                                                 // so throw an exeption if number of elements in enum does not 
01673                                                 // equal number of elements in [Field Size] table
01674                                                 if (Enum.GetNames(typeof(Field.FieldSize)).Length != fieldSizes.Count)
01675                                                         throw new Exception("There are field sizes which exist in Enum Field.FieldSize but does not exist in DB. The running DB is not up to date with respect to the ASP server.");
01676                                         }
01677                                 }
01678                         }
01679 
01680                         return fieldSizes;
01681                 }
01687                 static NecessityTypes GetNecessityTypes()
01688                 {
01689                         NecessityTypes necessityTypes = new NecessityTypes();
01690                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01691                         {
01692                                 sqlConnection.Open();
01693 
01694                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetNecessityTypes, sqlConnection))
01695                                 {
01696                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01697 
01698                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01699                                         {
01700                                                 while (sqlReader.Read())
01701                                                 {
01702                                                         Field.NecessityTypeData necessityTypeData = new Field.NecessityTypeData(sqlReader);
01703 
01704                                                         Field.NecessityType necessityType;
01705                                                         try
01706                                                         {
01707                                                                 // Next operator throw an exeption if we do not have 
01708                                                                 // element in enum which corresponds to necessity type in Necessity Type table
01709                                                                 // Catch it and throw exception with more clear message.
01710                                                                 necessityType = necessityTypeData.Type;
01711                                                         }
01712                                                         catch
01713                                                         {
01714                                                                 throw new Exception("Necessity type " + necessityTypeData.VisibleName + " exists in DB but does not exist in Enum Field.NecessityType. The running ASP server is not up to date with respect to the DB.");
01715                                                         }
01716 
01717                                                         necessityTypes.Add(necessityTypeData.VisibleName, necessityTypeData);
01718                                                 }
01719 
01720                                                 // We checked that all enum elements exist in [Necessity Type] table, 
01721                                                 // so throw an exeption if number of elements in enum does not 
01722                                                 // equal number of elements in [Necessity Type] table
01723                                                 if (Enum.GetNames(typeof(Field.NecessityType)).Length != necessityTypes.Count)
01724                                                         throw new Exception("There are necessity types which exist in Enum Field.NecessityType but does not exist in DB. The running DB is not up to date with respect to the ASP server.");
01725                                         }
01726                                 }
01727                         }
01728 
01729                         return necessityTypes;
01730                 }
01736                 static TableGroups GetTableGroups()
01737                 {
01738                         TableGroups tableGroups = new TableGroups();
01739 
01740                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01741                         {
01742                                 sqlConnection.Open();
01743 
01744                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetTableGroups, sqlConnection))
01745                                 {
01746                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01747 
01748                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01749                                         {
01750                                                 while (sqlReader.Read())
01751                                                 {
01752                                                         TableGroup tableGroup = new TableGroup(sqlReader);
01753                                                         tableGroups.Add(tableGroup.Name, tableGroup);
01754                                                 }
01755                                         }
01756                                 }
01757                         }
01758 
01759                         return tableGroups;
01760                 }
01766                 static QueryTypes GetQueryTypes()
01767                 {
01768                         QueryTypes queryTypes = new QueryTypes();
01769                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01770                         {
01771                                 sqlConnection.Open();
01772 
01773 
01774                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetQueryTypes, sqlConnection))
01775                                 {
01776                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01777 
01778                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01779                                         {
01780                                                 while (sqlReader.Read())
01781                                                 {
01782                                                         Query.QueryTypeData queryTypeData = new Query.QueryTypeData(sqlReader);
01783 
01784                                                         Query.QueryType queryType;
01785                                                         try
01786                                                         {
01787                                                                 // Next operator throw an exeption if we do not have 
01788                                                                 // element in enum which corresponds to query parameter type in [Query Type] table
01789                                                                 // Catch it and throw exception with more clear message.
01790                                                                 queryType = queryTypeData.Type;
01791                                                         }
01792                                                         catch
01793                                                         {
01794                                                                 throw new Exception("Query type " + queryTypeData.VisibleName + " exists in DB but does not exist in Enum Query.QueryType. The running ASP server is not up to date with respect to the DB.");
01795                                                         }
01796 
01797                                                         queryTypes.Add(queryTypeData.VisibleName, queryTypeData);
01798                                                 }
01799 
01800                                                 // We checked that all enum elements exist in [Query Type] table, 
01801                                                 // so throw an exeption if number of elements in enum does not 
01802                                                 // equal number of elements in [Query Type] table
01803                                                 if (Enum.GetNames(typeof(Query.QueryType)).Length != queryTypes.Count)
01804                                                         throw new Exception("There are query types which exist in Enum Query.QueryType but does not exist in DB. The running DB is not up to date with respect to the ASP server.");
01805                                         }
01806                                 }
01807                         }
01808 
01809                         return queryTypes;
01810                 }
01816                 static QueryParameterTypes GetQueryParameterTypes()
01817                 {
01818                         QueryParameterTypes queryParameterTypes = new QueryParameterTypes();
01819                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01820                         {
01821                                 sqlConnection.Open();
01822 
01823                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetQueryParameterTypes, sqlConnection))
01824                                 {
01825                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01826 
01827                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01828                                         {
01829                                                 while (sqlReader.Read())
01830                                                 {
01831                                                         QueryParameter.QueryParameterTypeData queryParameterTypeData = new QueryParameter.QueryParameterTypeData(sqlReader);
01832 
01833                                                         QueryParameter.QueryParameterType queryParameterType;
01834                                                         try
01835                                                         {
01836                                                                 // Next operator throw an exeption if we do not have 
01837                                                                 // element in enum which corresponds to query parameter type in [Query Parameter Type] table
01838                                                                 // Catch it and throw exception with more clear message.
01839                                                                 queryParameterType = queryParameterTypeData.Type;
01840                                                         }
01841                                                         catch
01842                                                         {
01843                                                                 throw new Exception("Query parameter type " + queryParameterTypeData.VisibleName + " exists in DB but does not exist in Enum QueryParameter.QueryParameterType. The running ASP server is not up to date with respect to the DB.");
01844                                                         }
01845 
01846                                                         queryParameterTypes.Add(queryParameterTypeData.VisibleName, queryParameterTypeData);
01847                                                 }
01848 
01849                                                 // We checked that all enum elements exist in [Query Parameter Type] table, 
01850                                                 // so throw an exeption if number of elements in enum does not 
01851                                                 // equal number of elements in [Query Parameter Type] table
01852                                                 if (Enum.GetNames(typeof(QueryParameter.QueryParameterType)).Length != queryParameterTypes.Count)
01853                                                         throw new Exception("There are query parameter types which exist in Enum QueryParameter.QueryParameterType but does not exist in DB. The running DB is not up to date with respect to the ASP server.");
01854                                         }
01855                                 }
01856                         }
01857 
01858                         return queryParameterTypes;
01859                 }
01865         static Queries GetQueries(Query.QueryTypeData queryTypeData)
01866                 {
01867                         Queries queries = new Queries();
01868 
01869                         using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
01870                         {
01871                                 sqlConnection.Open();
01872 
01873                                 using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_GetQueries, sqlConnection))
01874                                 {
01875                                         sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
01876                                         if (queryTypeData != null)
01877                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryTypeID, queryTypeData.ID);
01878 
01879                                         using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
01880                                         {
01881                                                 while (sqlReader.Read())
01882                                                 {
01883                                                         Query query = new Query(sqlReader);
01884                             //query.QueryParameters = GetQueryParametersByQuery(query);
01885                                                         queries.Add(query.Name, query);
01886                                                 }
01887                                         }
01888                                 }
01889                 foreach (var query in queries.Values)
01890                     query.QueryParameters = GetQueryParametersByQuery(query, sqlConnection);
01891                         }
01892                         return queries;
01893                 }
01894 
01895                 static string GetSqlTypeByParameterType(QueryParameter.QueryParameterType type)
01896                 {
01897                         string sqlType = string.Empty;
01898                         switch (type)
01899                         {
01900                                 case QueryParameter.QueryParameterType.Integer:
01901                                 case QueryParameter.QueryParameterType.Lookup:
01902                                 case QueryParameter.QueryParameterType.LookupFromTable:
01903                                 case QueryParameter.QueryParameterType.LookupFromQuery:
01904                                 case QueryParameter.QueryParameterType.PrimaryKey:
01905                                 case QueryParameter.QueryParameterType.PreselectedFacility:
01906                                 case QueryParameter.QueryParameterType.PreselectedDepartment:
01907                                 case QueryParameter.QueryParameterType.UserID:
01908                                         sqlType = "int";
01909                                         break;
01910                                 case QueryParameter.QueryParameterType.Text:
01911                                         sqlType = "nvarchar(max)";
01912                                         break;
01913                                 case QueryParameter.QueryParameterType.Date:
01914                                 case QueryParameter.QueryParameterType.DatesRangeEndDate:
01915                                 case QueryParameter.QueryParameterType.DatesRangeStartDate:
01916                                         sqlType = "datetime";
01917                                         break;
01918                                 case QueryParameter.QueryParameterType.Rational:
01919                                         sqlType = "float";
01920                                         break;
01921                                 case QueryParameter.QueryParameterType.Xml:
01922                                         sqlType = "xml";
01923                                         break;
01924                                 default:
01925                                         sqlType = "sql_variant";
01926                                         break;
01927                         }
01928                         return sqlType;
01929                 }
01930 
01931                 static string GetParameterValueByParameterType(QueryParameter.QueryParameterType type)
01932                 {
01933                         string parameterValue = string.Empty;
01934                         switch (type)
01935                         {
01936                                 case QueryParameter.QueryParameterType.Integer:
01937                                 case QueryParameter.QueryParameterType.Lookup:
01938                                 case QueryParameter.QueryParameterType.LookupFromTable:
01939                                 case QueryParameter.QueryParameterType.LookupFromQuery:
01940                                 case QueryParameter.QueryParameterType.PrimaryKey:
01941                                 case QueryParameter.QueryParameterType.Rational:
01942                                 case QueryParameter.QueryParameterType.PreselectedFacility:
01943                                 case QueryParameter.QueryParameterType.PreselectedDepartment:
01944                                 case QueryParameter.QueryParameterType.UserID:
01945                                         parameterValue = 0.ToString();
01946                                         break;
01947                                 case QueryParameter.QueryParameterType.Text:
01948                                 case QueryParameter.QueryParameterType.Xml:
01949                                         parameterValue = "''";
01950                                         break;
01951                                 case QueryParameter.QueryParameterType.Date:
01952                                 case QueryParameter.QueryParameterType.DatesRangeEndDate:
01953                                 case QueryParameter.QueryParameterType.DatesRangeStartDate:
01954                                         //parameterValue = string.Format("'{0}'", DateTime.Now.ToString());
01955                                         parameterValue = "GETDATE()";
01956                                         break;
01957                                 default:
01958                                         parameterValue = "null";
01959                                         break;
01960                         }
01961                         return parameterValue;
01962                 }
01963 
01964                 static string GetSqlTypeByColumnType(string type)
01965                 {
01966                         string sqlType = string.Empty;
01967                         switch (type)
01968                         {
01969                                 case "System.Int32":
01970                                         sqlType = "int";
01971                                         break;
01972                                 case "System.String":
01973                                         sqlType = "nvarchar(max)";
01974                                         break;
01975                                 case "System.DateTime":
01976                                         sqlType = "datetime";
01977                                         break;
01978                                 case "System.Single":
01979                                         sqlType = "real";
01980                                         break;
01981                                 case "System.Double":
01982                                         sqlType = "float";
01983                                         break;
01984                                 default:
01985                                         sqlType = "sql_variant";
01986                                         break;
01987                         }
01988                         return sqlType;
01989                 }
01990 
01991                 static bool ValidateForbiddenCommandsInQuery(Query query, DataSet dataSet, SqlDataAdapter dataAdapter, ref string returnMessage)
01992                 {
01993                         bool success = true;
01994                         StringBuilder sqlStr = new StringBuilder();
01995                         try
01996                         {
01997                                 // Drop function if exists
01998                                 sqlStr.AppendLine(string.Format("IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{0}]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))", query.Name));
01999                                 sqlStr.AppendLine(string.Format("       DROP FUNCTION [dbo].[{0}]", query.Name));
02000                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
02001                                 dataAdapter.Fill(dataSet);
02002 
02003                                 // Create function
02004                                 sqlStr.Length = 0;
02005                                 sqlStr.AppendLine(string.Format("/******************************************************************************"));
02006                                 sqlStr.AppendLine(string.Format("**  Function Name: [{0}]", query.Name));
02007                                 sqlStr.AppendLine(string.Format("******************************************************************************/"));
02008                                 sqlStr.AppendLine(string.Format("CREATE Function [{0}](", query.Name));
02009                                 sqlStr.AppendLine(string.Format(")"));
02010                                 sqlStr.AppendLine(string.Format("RETURNS @ResultTable TABLE"));
02011                                 sqlStr.AppendLine(string.Format("("));
02012                                 sqlStr.AppendLine(string.Format("       [TestColumn] [int] NOT NULL"));
02013                                 sqlStr.AppendLine(string.Format(")"));
02014                                 sqlStr.AppendLine(string.Format("AS"));
02015                                 sqlStr.AppendLine(string.Format("BEGIN"));
02016                                 sqlStr.AppendLine(string.Format("       {0}", query.QueryText));
02017                                 sqlStr.AppendLine(string.Format("       INSERT @ResultTable"));
02018                                 sqlStr.AppendLine(string.Format("       SELECT 0"));
02019                                 sqlStr.AppendLine(string.Format(""));
02020                                 sqlStr.AppendLine(string.Format("       RETURN"));
02021                                 sqlStr.AppendLine(string.Format("END"));
02022 
02023                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
02024                                 dataAdapter.Fill(dataSet);
02025 
02026                                 returnMessage = string.Empty;
02027                         }
02028                         catch(SqlException excep)
02029                         {
02030                                 foreach (SqlError err in excep.Errors)
02031                                 {
02032                                         if (err.Number == 443)
02033                                         {
02034                                                 success = false;
02035                                                 returnMessage = Common.Messages.QUERY_ERROR_FORBIDDEN_STATEMENTS;
02036                                                 break;
02037                                         }
02038                                 }
02039                         }
02040                         catch (Exception e)
02041                         {
02042                                 success = false;
02043                                 returnMessage = e.Message;
02044                         }
02045                         return success;
02046                 }
02053                 static bool SaveQuery_SetCompiled(Query query, bool compiled, ref string returnMessage)
02054                 {
02055                         try
02056                         {
02057                                 using (SqlConnection sqlConnection = new SqlConnection(DataAccessUtilities.ConnectionString))
02058                                 {
02059                                         sqlConnection.Open();
02060 
02061                                         using (SqlCommand sqlCommand = new SqlCommand(Constants.SP_NAME_UpdateQueryCompiled, sqlConnection))
02062                                         {
02063                                                 sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;
02064                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_QueryID, query.ID);
02065                                                 DataAccessUtilities.AddParameter(sqlCommand, Constants.SP_PARAMETER_Compiled, compiled);
02066 
02067                                                 sqlCommand.ExecuteNonQuery();
02068                                                 query.Compiled = compiled;
02069                                         }
02070                                 }
02071                                 returnMessage = RAIS.Common.Messages.QUERY_SAVED;
02072                                 return true;
02073                         }
02074                         catch (Exception excep)
02075                         {
02076                 returnMessage = excep.Message + Constants.DATA_BASE_ERROR_MASSAGE;
02077                                 return false;
02078                         }
02079                 }
02080 
02081                 static bool CreateStoredProcedure(Query query, DataSet dataSet, SqlDataAdapter dataAdapter, ref string returnMessage)
02082                 {
02083                         StringBuilder sqlStr = new StringBuilder();
02084                         bool success = true;
02085                         try
02086                         {
02087                                 // Drop function if exists
02088                                 sqlStr.AppendLine(string.Format("IF  EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = '{0}')", query.Name));
02089                                 sqlStr.AppendLine(string.Format("       DROP PROCEDURE [dbo].[{0}]", query.Name));
02090                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
02091                                 dataAdapter.Fill(dataSet);
02092 
02093                                 // Create function
02094                                 sqlStr.Length = 0;
02095                                 sqlStr.AppendLine(string.Format("/******************************************************************************"));
02096                                 sqlStr.AppendLine(string.Format("**  Stored Procedure Name: [{0}]", query.Name));
02097                                 sqlStr.AppendLine(string.Format("******************************************************************************/"));
02098                                 sqlStr.AppendLine(string.Format("CREATE Procedure [{0}]", query.Name));
02099 
02100                 sqlStr.AppendLine(CreateParametersBlock(query));
02101 
02102                                 sqlStr.AppendLine(string.Format("AS"));
02103                                 sqlStr.AppendLine(string.Format("       {0}", query.QueryText));
02104 
02105                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
02106                                 dataAdapter.Fill(dataSet);
02107 
02108                                 // Grant select for public
02109                                 sqlStr.Length = 0;
02110                                 sqlStr.AppendLine(string.Format("GRANT EXEC ON [{0}] TO PUBLIC", query.Name));
02111                                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
02112                                 dataAdapter.Fill(dataSet);
02113 
02114                                 // Test Stored Procedure
02115                 sqlStr.Length = 0;
02116                 sqlStr.Append(string.Format("EXEC [{0}]", query.Name));
02117 
02118                 for (int i = 0; i < query.QueryParameters.Count; i++)
02119                     if(i==query.QueryParameters.Count-1)
02120                         sqlStr.Append("0");
02121                     else
02122                         sqlStr.Append("0,");
02123 
02124 
02125                 dataAdapter.SelectCommand.CommandText = sqlStr.ToString();
02126                                 dataAdapter.Fill(dataSet);
02127 
02128                                 success = SaveQuery_SetCompiled(query, true, ref returnMessage);
02129 
02130                                 if (success)
02131                                         returnMessage = Common.Messages.QUERY_CREATED;
02132                         }
02133                         catch (Exception e)
02134                         {
02135                                 success = false;
02136                 returnMessage = e.Message;
02137                         }
02138 
02139                         return success;
02140                 }
02141 
02142         private static string CreateParametersBlock(Query query)
02143         {
02144             bool firstParameter = true;
02145             StringBuilder sqlStr=new StringBuilder();
02146             foreach (KeyValuePair<string, QueryParameter> kvp in query.QueryParameters)
02147             {
02148                 QueryParameter parameter = kvp.Value;
02149                 sqlStr.AppendLine(string.Format("{0} @{1} {2}"
02150                     , (firstParameter) ? "        " : " , "
02151                     , parameter.Name
02152                     , GetSqlTypeByParameterType(parameter.Type)));
02153                 firstParameter = false;
02154             }
02155             return sqlStr.ToString();
02156         }
02157                 #endregion
02158         }
02159 }